package com.my.zl.console.service.visualEdit.impl;

import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.my.zl.common.dto.ReqDTO;
import com.my.zl.common.dto.advancedQuery.PageQueryDTO;
import com.my.zl.common.enums.resp.GlobalEnum;
import com.my.zl.common.exception.BizException;
import com.my.zl.common.utils.CopyUtil;
import com.my.zl.console.common.constant.AutocodeC;
import com.my.zl.console.dao.visualEdit.ZlHtmlDesignDtlMapper;
import com.my.zl.console.dto.visualEdit.htmlDesignDtl.ReqHtmlDesignDtlAddDTO;
import com.my.zl.console.dto.visualEdit.htmlDesignDtl.ReqHtmlDesignDtlDelDTO;
import com.my.zl.console.dto.visualEdit.htmlDesignDtl.ReqHtmlDesignDtlDetailDTO;
import com.my.zl.console.dto.visualEdit.htmlDesignDtl.ReqHtmlDesignDtlEditDTO;
import com.my.zl.console.dto.visualEdit.htmlDesignDtl.ReqHtmlDesignDtlQueryDTO;
import com.my.zl.console.dto.visualEdit.htmlDesignDtl.ReqHtmlDesignDtlStateDTO;
import com.my.zl.console.dto.visualEdit.htmlDesignDtl.ResHtmlDesignDtlAddDTO;
import com.my.zl.console.dto.visualEdit.htmlDesignDtl.ResHtmlDesignDtlDelDTO;
import com.my.zl.console.dto.visualEdit.htmlDesignDtl.ResHtmlDesignDtlDetailDTO;
import com.my.zl.console.dto.visualEdit.htmlDesignDtl.ResHtmlDesignDtlEditDTO;
import com.my.zl.console.dto.visualEdit.htmlDesignDtl.ResHtmlDesignDtlQueryDTO;
import com.my.zl.console.dto.visualEdit.htmlDesignDtl.ResHtmlDesignDtlStateDTO;
import com.my.zl.console.dto.visualEdit.htmlDesignDtl.ResHtmlDesignDtlVO2;
import com.my.zl.console.entity.visualEdit.ZlHtmlDesignDtl;
import com.my.zl.console.po.visualEdit.htmlDesignDtl.ZlHtmlDesignDtlVo;
import com.my.zl.console.service.base.BaseManager;
import com.my.zl.console.service.visualEdit.IHtmlDesignDtlService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 在线页面明细
 * @author ZL
 */
@Service
public class HtmlDesignDtlServiceImpl extends BaseManager implements IHtmlDesignDtlService {

    @Autowired
    private ZlHtmlDesignDtlMapper htmlDesignDtlMapper;

    @Override
    public ResHtmlDesignDtlQueryDTO queryHtmlDesignDtlList(ReqHtmlDesignDtlQueryDTO reqInfo,PageQueryDTO page) throws Exception{
        // TODO Auto-generated method stub
        ResHtmlDesignDtlQueryDTO resDTO = BeanUtil.copyProperties(page, ResHtmlDesignDtlQueryDTO.class);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        
        pageHandle(page);//处理分页数据
        //当效率不行时自行计数，或完全自行分页，先查询总数，如果为0不用再次查询了；开启计数：true是,false否
        //临近第一条SQL查询语句自动分页,如果未消费时(即后面没有触发SQL查询)则需主动调用pageHp.clear()清理
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlHtmlDesignDtlVo> list = htmlDesignDtlMapper.selectListPage(paramMap);
        resDTO.setData(CopyUtil.copyList(list, ResHtmlDesignDtlVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResHtmlDesignDtlDetailDTO queryHtmlDesignDtlDetail(ReqHtmlDesignDtlDetailDTO reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ResHtmlDesignDtlDetailDTO res = new ResHtmlDesignDtlDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        ZlHtmlDesignDtl view = htmlDesignDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResHtmlDesignDtlAddDTO insertHtmlDesignDtlInfo(ReqDTO<ReqHtmlDesignDtlAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqHtmlDesignDtlAddDTO reqData = reqInfo.getReqData();
        ResHtmlDesignDtlAddDTO res = new ResHtmlDesignDtlAddDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlHtmlDesignDtl record = new ZlHtmlDesignDtl();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setHtmlDtlId(IdUtil.fastSimpleUUID());
        record.setDataStatus(AutocodeC.DataStatus.ENABLE.getCode());//状态
        record.setRandomCode(StrUtil.uuid().substring(0,8));
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("htmlDtlId",record.getHtmlDtlId());
        //检查主键是否冲突
        if(htmlDesignDtlMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalEnum.DUPLICATE_PRIMARY_KEY);
        }
        record.setRandomCode(StrUtil.uuid().substring(0,8));
        htmlDesignDtlMapper.insertSelective(record);
        
        ZlHtmlDesignDtl keyInfo = htmlDesignDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResHtmlDesignDtlEditDTO updateHtmlDesignDtlInfo(ReqDTO<ReqHtmlDesignDtlEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqHtmlDesignDtlEditDTO reqData = reqInfo.getReqData();
        ResHtmlDesignDtlEditDTO res = new ResHtmlDesignDtlEditDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlHtmlDesignDtl record = new ZlHtmlDesignDtl();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        record.setRandomCode(StrUtil.uuid().substring(0,8));
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("htmlDtlId",record.getHtmlDtlId());
        paramMap.put("versionNum", record.getVersionNum());
        paramMap.put("randomCode", record.getRandomCode());
        htmlDesignDtlMapper.updateByMapSelective(record,paramMap);
        
        paramMap.clear();
        paramMap.put("htmlDtlId",record.getHtmlDtlId());
        ZlHtmlDesignDtl keyInfo = htmlDesignDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResHtmlDesignDtlDelDTO deleteHtmlDesignDtlInfo(ReqDTO<ReqHtmlDesignDtlDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqHtmlDesignDtlDelDTO reqData = reqInfo.getReqData();
        ResHtmlDesignDtlDelDTO res = new ResHtmlDesignDtlDelDTO();
        Instant time = getSystemTime().getUtcTime();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        htmlDesignDtlMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResHtmlDesignDtlStateDTO updateHtmlDesignDtlState(ReqDTO<ReqHtmlDesignDtlStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqHtmlDesignDtlStateDTO reqData = reqInfo.getReqData();
        ResHtmlDesignDtlStateDTO res = new ResHtmlDesignDtlStateDTO();
        Instant time = getSystemTime().getUtcTime();
         
        ZlHtmlDesignDtl record = new ZlHtmlDesignDtl();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        htmlDesignDtlMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("htmlDtlId",record.getHtmlDtlId());
        ZlHtmlDesignDtl keyInfo = htmlDesignDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}